package com.situ.mymall.controller;

import com.situ.mymall.pojo.entity.MallProvince;
import com.situ.mymall.pojo.entity.User;
import com.situ.mymall.service.IUserService;
import com.situ.mymall.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public Result login(String name, String password, HttpSession session){
        User user = userService.login(name,password);
        if (user == null){
            return Result.error("账号或密码输入错误");
        }
        session.setAttribute("user",user);
        return Result.ok("登录成功");
    }

    //商品加入购物车时，先判断用户是否登录
    @RequestMapping("/checkUserLogin")
    public Result checkUserLogin(HttpSession session){
        User user = (User) session.getAttribute("user");
        return user != null ? Result.ok() : Result.error();
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        session.removeAttribute("user");
        return Result.ok("退出成功");
    }

    @RequestMapping("/add")
    public Result add(User user){
        userService.add(user);
        return Result.ok("注册成功,请重新登陆！！！");
    }

    //省
    @RequestMapping("/selectByProvince")
    public Result selectByProvince(){
        List<MallProvince> list = userService.selectByProvince();
        return Result.ok(list);
    }
    //市
    @RequestMapping("/selectByCity")
    public Result selectByCity(Long provinceId){
        List<MallProvince> list = userService.selectByCity(provinceId);
        return Result.ok(list);
    }
    // 区/县
    @RequestMapping("/selectByArea")
    public Result selectByArea(Integer cityId){
        List<MallProvince> list = userService.selectByArea(cityId);
        return Result.ok(list);
    }




}
